/*******************************************************************************
 * @file        eeprom_24aa.h
 * @brief       C library to access the 24AA** family of EEPROM chips with
 *              unique internal EUI48 (MAC address)
 * @version
 * @author      Simon Burkhardt
 * @date        2021-11-19
 * @copyright   (c) 2021 eta systems GmbH
*******************************************************************************/

/*
 * The 6-byte EUI-48™ node address value of the
 * 24AAXXXE48 is stored in array locations 0xFA through
 * 0xFF, as shown in Figure 9-2. The first three bytes are
 * the Organizationally Unique Identifier (OUI) assigned
 * to Microchip by the IEEE Registration Authority. The
 * remaining three bytes are the Extension Identifier, and
 * are generated by Microchip to ensure a globally
 * unique, 48-bit value.
 */

#ifndef _EEPROM_24AA_H
#define	_EEPROM_24AA_H

#include "stm32f7xx_hal.h"
extern I2C_HandleTypeDef hi2c1;

/* Defines */
#define EEPROM24AA_ADDRESS 0xA0  // as tested 0xA0 ... 0xAF
#define EEPROM24AA_REG_IP      0x10
#define EEPROM24AA_REG_GATEWAY 0x14
#define EEPROM24AA_REG_SUBNET  0x18
#define EEPROM24AA_REG_DHCP_EN 0x1B

#define EEPROM24AA_TIMEOUT     100 // HAL_MAX_DELAY

// #define EEPROM24AA_PRINT_DEBUG

/* Macros */
#define _EEPROM_GetEUI48(adr, dst) HAL_I2C_Mem_Read(&hi2c1, adr, 0xFA, sizeof(uint8_t), dst, 6, EEPROM24AA_TIMEOUT);
#define _EEPROM_SaveByte(adr, b)   HAL_I2C_Mem_Write( &hi2c1, EEPROM24AA_ADDRESS, adr, sizeof(uint8_t), (uint8_t*)&b, 1, EEPROM24AA_TIMEOUT);
#define _EEPROM_ReadByte(adr, b)   HAL_I2C_Mem_Read ( &hi2c1, EEPROM24AA_ADDRESS, adr, sizeof(uint8_t), (uint8_t*)&b, 1, EEPROM24AA_TIMEOUT);
#define _EEPROM_SaveIP(adr, ip)    HAL_I2C_Mem_Write( &hi2c1, EEPROM24AA_ADDRESS, adr, sizeof(uint8_t), (uint8_t*)&ip, 4, EEPROM24AA_TIMEOUT);
#define _EEPROM_ReadIP(adr, ip)    HAL_I2C_Mem_Read ( &hi2c1, EEPROM24AA_ADDRESS, adr, sizeof(uint8_t), (uint8_t*)&ip, 4, EEPROM24AA_TIMEOUT);

/* Function Prototypes */
void    EEPROM_SaveByte(uint8_t adr, uint8_t b);
uint8_t  EEPROM_ReadByte(uint8_t adr);
void    EEPROM_SaveIP(uint8_t adr, uint32_t ip);
uint32_t EEPROM_ReadIP(uint8_t adr);


/*
HAL_I2C_Mem_Write( &hi2c1, EEPROM24AA_ADDRESS, MEMORY_ADDRESS, sizeof(uint8_t), src, length, 1000);
HAL_I2C_Mem_Read ( &hi2c1, EEPROM24AA_ADDRESS, MEMORY_ADDRESS, sizeof(uint8_t), dst, length, 1000);
*/

#endif	/* _EEPROM_24AA_H */
